package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.dao.CheckGroupItemDao;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckGroupItem;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.result.PageResult;
import com.itheima.health.result.QueryPageBean;
import com.itheima.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;

    @Autowired
    private CheckGroupItemDao checkGroupItemDao;

    @Autowired
    private CheckItemDao checkItemDao;

    @Transactional
    @Override
    public void add(List<Integer> checkitemIds, CheckGroup checkGroup) {
        log.info("[检查组-添加]data：{}",checkGroup);
        checkGroupDao.insert(checkGroup);

        List<CheckGroupItem> checkGroupItems = new ArrayList<>();
        List<CheckItem> checkItems = new ArrayList<>();
        for (Integer checkitemId : checkitemIds) {
            CheckItem checkItem = checkItemDao.selectById(checkitemId);
            checkItems.add(checkItem);
        }

        for (CheckItem checkItem : checkItems) {
            CheckGroupItem checkGroupItem = new CheckGroupItem();
            checkGroupItem.setCheckgroupId(checkGroup.getId());
            checkGroupItem.setCheckitemId(checkItem.getId());
            checkGroupItem.setCode(checkItem.getCode());
            checkGroupItem.setName(checkItem.getName());
            checkGroupItem.setRemark(checkItem.getRemark());

            checkGroupItems.add(checkGroupItem);

           checkGroupItemDao.insert(checkGroupItem);
        }
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        log.info("[检查组-分页查询]data：{}",queryPageBean);
        //使用分页插件


        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        // 分页查询数据库
        Page<CheckGroup> page = checkGroupDao.selectByConditon(queryPageBean.getQueryString());
        //封装分页数据t_checkgroup
        return new PageResult(page.getTotal(),page.getResult());
    }



    @Transactional
    @Override
    public void delete(int id) {
        log.info("[检查组-根据id删除]id：{}",id);
        //TODO 校验是否可以删除
        checkGroupDao.deleteById(id);

        checkGroupItemDao.deleteByCheckgroupId(id);
    }

    @Transactional
    @Override
    public void edit(List<Integer> checkitemIds, CheckGroup checkGroup) {
        log.info("[检查组-编辑]data:{}",checkGroup);
        checkGroupDao.update(checkGroup);


        checkGroupItemDao.deleteByCheckgroupId(checkGroup.getId()); //将以前的关系删掉

        //遍历新的项组关系
        for (Integer checkitemId : checkitemIds) {
            //通过项的id查到项的对象
            CheckItem checkItem = checkItemDao.selectById(checkitemId);

            //这些属性就可以通过项的对象和组的对象就获得
            CheckGroupItem checkGroupItem = new CheckGroupItem();
            checkGroupItem.setCheckgroupId(checkGroup.getId());
            checkGroupItem.setCheckitemId(checkItem.getId());
            checkGroupItem.setCode(checkGroup.getCode());
            checkGroupItem.setName(checkItem.getName());
            checkGroupItem.setRemark(checkItem.getRemark());

            checkGroupItemDao.insert(checkGroupItem);

        }
    }



    @Override
    public CheckGroup findById(Integer id) {
        log.info("[检查组-根据ID查询]id:{}",id);
        return checkGroupDao.selectById(id);
    }

    @Override
    public List<CheckGroup> findAll() {
        log.info("[检查组-查询所有]~");
        return checkGroupDao.selectAll();
    }

    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id){
        List<CheckGroupItem> checkGroupItems = checkGroupItemDao.getByCheckgroupId(id);
        List<Integer> ids = new ArrayList<>();
        for (CheckGroupItem checkGroupItem : checkGroupItems) {
            //根据检查组的id获取检查项的id根据检查项获取每个id
            ids.add(checkGroupItem.getCheckitemId());
        }
        return ids;
    }
}
